#!/usr/bin/env bash

################################################################################
#
# Copyright (C) 2013  Artur Tolstenco
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 
################################################################################

# =============================================================================
#
# Author:       Artur Tolstenco
# Date:		    15/05/2013
# Description:	This script creates (or deletes) the comune mysql user and its 
#               two databses 
#               After the creation of databases this script populates both
#		        databases using the files present in the folder
#
# =============================================================================

CURRENT_DIR=$(dirname "$0")
EXPECTED_ARGS=2
CLEAN_EXPECTED_ARGS=2
CREATE_EXPECTED_ARGS=3

PAR_CREATE='create'
PAR_CLEAN='clean'

PAR_DEPLOY_ON_DIONE='dione'
PAR_DEPLOY_ON_LOCAL='local'

PAR_ALL='all'
PAR_CREATE_USER='create_user'
PAR_POPULATION='populate'


# mysql user to create
DB_USER='agterritorio'
DB_USER_PWD=$DB_USER'123'

# the specific resource server instantiation
DATA_INSTANCE='agenziaterritorio'

# the two databases to create for this resource server instantiation
RS_DATABASE='agterritorio_rs'
DATA_DATABASE='agterritorio_dati'

# the $RS_DATABASE population script files
RS_DB_SCHEMA=$CURRENT_DIR'/rs.schema.mysql.sql'
RS_DB_DATA=$CURRENT_DIR'/rs.data.mysql.sql'

# the $DATI_DATABASE population script files
DATA_DB_SCHEMA=$CURRENT_DIR'/'$DATA_INSTANCE'.schema.mysql.sql'
DATA_DB_DATA=$CURRENT_DIR'/'$DATA_INSTANCE'.data.mysql.sql'


# PRIVATE FUNCTIONS ############################################################

# checks if there was an error during execution of previous comand 
# and prints the $1 message
function check_error() {
    if [ $? -gt 0 ]; then
		echo $1
		exit 1
	fi
}

################################################################################


# MAIN #########################################################################


if [ $# -lt $EXPECTED_ARGS ]; then
    echo 'Error: some arguments are missing'
	echo 'Usage: '$(basename $0)' '$PAR_CLEAN' '$PAR_DEPLOY_ON_DIONE'|'$PAR_DEPLOY_ON_LOCAL
    echo 'Usage: '$(basename $0)' '$PAR_CREATE\
                                   $PAR_DEPLOY_ON_DIONE'|'$PAR_DEPLOY_ON_LOCAL\
                                   $PAR_ALL'|'$PAR_CREATE_USER'|'$PAR_POPULATION
	exit 1
fi

cp $CURRENT_DIR'/application.ini' $CURRENT_DIR'/../../application/configs/application.ini'
check_error 'error copying the application.ini'

case $1 in
    $PAR_CLEAN)
        if [ $# -ne $CLEAN_EXPECTED_ARGS ]; then
	        echo 'Error: some arguments are missing'
	        echo 'Usage: '$(basename $0)' '$PAR_CLEAN' '$PAR_DEPLOY_ON_DIONE'|'$PAR_DEPLOY_ON_LOCAL
	        exit 1
        fi
        
        bash $CURRENT_DIR'/../'set_up.sh $1 $2 $DB_USER $DB_USER_PWD $RS_DATABASE $DATA_DATABASE
        check_error 'Error doing clean'
        ;;
    $PAR_CREATE)
        if [ $# -ne $CREATE_EXPECTED_ARGS ]; then
	        echo 'Error: some arguments are missing'
	        echo 'Usage: '$(basename $0)' '$PAR_CREATE\
	                                       $PAR_DEPLOY_ON_DIONE'|'$PAR_DEPLOY_ON_LOCAL\
	                                       $PAR_ALL'|'$PAR_CREATE_USER'|'$PAR_POPULATION
	        exit 1
        fi
        
        bash $CURRENT_DIR'/../'set_up.sh $1 $2 $3 $DB_USER $DB_USER_PWD $DATA_INSTANCE \
                          $RS_DATABASE $DATA_DATABASE \
                          $RS_DB_SCHEMA $RS_DB_DATA \
                          $DATA_DB_SCHEMA $DATA_DB_DATA
        check_error 'Error doing create'
        ;;
    *)
        echo 'Unrecognized parameter. Allowed parameters: '$PAR_CREATE', '$PAR_CLEAN
esac

exit 0
